package com.tueye.web.interceptor;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.tueye.entity.Admin;
import com.tueye.utils.TuEyeConstant;
/**
 * Title:      TuEyeAdminFilter.java
 * Description:过滤pages/admin下的所有jsp页面
 * 	如果session中有TuEyeContent.Admin对象就过，不然就转登陆界面
 * @author yuyue
 *
 */
public class TuEyeAdminFilter extends HttpServlet implements Filter {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Override
	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain filterChain) throws IOException, ServletException {
		// TODO Auto-generated method stub
		HttpServletRequest httpRequest = (HttpServletRequest) request; 
		HttpServletResponse httpResponse = (HttpServletResponse) response; 
		boolean isValid = false; 
		String uriStr = httpRequest.getRequestURI();
		Admin admin  = (Admin) httpRequest.getSession().getAttribute(TuEyeConstant.TUEYE_ADMIN);
//		System.out.println(admin+"ddd");
//		System.out.println(uriStr);
		
		if (uriStr.indexOf("logonadmin.jsp") != -1 || httpRequest.getSession().getAttribute(TuEyeConstant.TUEYE_ADMIN) != null ) { 
		   isValid = true; 
		}else {
			isValid = false;
		}
		
		if (isValid == true) { 
		request.setCharacterEncoding("GBK"); 
		filterChain.doFilter(request, response); 
		}  
		else { 
		request.setCharacterEncoding("GBK"); 
		PrintWriter out = httpResponse.getWriter();  
		out.write("<script>window.location.href='"+httpRequest.getContextPath()+"/pages/admin/logonadmin.jsp'</script>"); 
	  } 


	}

	@Override
	public void init(FilterConfig arg0) throws ServletException {
		// TODO Auto-generated method stub

	}

	 
	

}
